<?php
/**
 * Created by PhpStorm.
 * User: hui
 * Date: 16/7/24
 * Time: 15:27
 */
namespace app\index\model;

use my\Security;
use think\Cookie;
use think\Db;
use think\Model;
use think\Session;

/**
 * Class Find 查询数据模块
 * @package app\index\model
 */
class Find extends Model
{
    /**
     * 用户唯一标识符
     * @var mixed|string
     */
    private static $uuid = '';

    /**
     * Find constructor. 设置唯一标识符
     * @param string $data
     */
    public function __construct($data = '')
    {
        parent::__construct($data);
        $uuid = Cookie::get('auth')['name'];
        $apiKey = Auth::get_api_key();
        $uuid = Security::decrypt($uuid, $apiKey);
        self::$uuid = Session::get('uuid') ? Session::get('uuid') : $uuid;
    }

    public function get_uuid()
    {
        return self::$uuid;
    }

    /**
     * 获得登录日志
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public function get_login()
    {
        $uuid = self::$uuid;
        $loginLog = LoginLog::where('uuid', $uuid)
            ->order('id desc')
            ->limit(2)
            ->select();
        if (!empty($loginLog)) {
            return $loginLog;
        }
        return false;
    }

    /**
     * 获得登录日间
     * @return int
     */
    public function get_loginTime()
    {
        $loginTime = $this->get_login();
        if (is_array($loginTime)) {
            if (count($loginTime) == 2) {
                return $loginTime = $loginTime[1]['time'];
            }
        }
        return 0;
    }

    /**
     * 获取积分信息
     * @return array|bool|false|\PDOStatement|string|Model
     */
    public function get_point()
    {
        $uuid = self::$uuid;
        $point = Db::table('u_card')
            ->alias('c')
            ->join('u_member m', 'c.ID = m.CardID')
            ->field('c.Point,c.AvailablePoint,c.IntroducerPoint')
            ->where('m.ID', $uuid)
            ->find();
        if ($point !== null) {
            $used[] = Db::table('r_income')
                ->alias('ri')
                ->join('u_income ui', 'ri.IncomeID = ui.ID')
                ->where('ri.MemberID', $uuid)
                ->sum('Exchange');
            $used[] = Db::table('u_exchange')
                ->where('MemberID', $uuid)
                ->sum('Point');
            $point['used'] = array_sum($used);
            $point['AvailablePoint'] = round($point['AvailablePoint']);
            $point['IntroducerPoint'] = round($point['IntroducerPoint']);
            return $point;
        }
        return false;
    }

    /**
     * 获取登录日志
     * @return \think\paginator\Collection
     */
    public function get_log($date = '')
    {
        $uuid = self::$uuid;
        if (empty($date)) {
            $log = LoginLog::where('uuid', $uuid)
                ->field('time,ip,country,province,city,district,carrier,browser,os')
                ->order('time', 'desc')
                ->paginate(15);
        } else {
            $log = LoginLog::where('uuid', $uuid)
                ->field('time,ip,country,province,city,district,carrier,browser,os')
                ->order('time', 'desc')
                ->where('time', 'between', [
                    strtotime($date['start_date']),
                    strtotime($date['end_date']),
                ])
                ->paginate(15);
        }

        return $log;
    }

    /**
     * 获取首页消费
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public function get_sell()
    {
        $uuid = self::$uuid;
        $sells = Db::table('u_sell')
            ->alias('s')
            ->join('u_personnel p', 's.PersonnelID = p.ID')
            ->field('s.CreateDate,s.TotalMoney,s.Point,s.Remark,p.Department,s.ID')
            ->where('s.MemberID', $uuid)
            ->order('s.CreateDate', 'desc')
            ->limit(5)
            ->select();
        if (empty($sells)) {
            return $sells = false;
        }
        $product = '';
        foreach ($sells as $key => $sell) {
            $product[$key] = $sell;
            $product[$key]['product'] = $this->get_sells_product($sell['ID']);
        }
        return $product;
    }


    /**
     * 获取全部消费
     * @return
     */
    public function get_sells()
    {
        $uuid = self::$uuid;
        $sells = Db::table('u_sell')
            ->alias('s')
            ->join('u_personnel p', 's.PersonnelID = p.ID')
            ->field('s.CreateDate,s.TotalMoney,s.Point,s.Remark,p.Department,s.ID')
            ->where('s.MemberID', $uuid)
            ->order('s.CreateDate', 'desc')
            ->paginate(15);
        if (empty($sells[0])) {
            return $sells = false;
        }
        $product[] = '';
        foreach ($sells as $key => $sell) {
            $product[$key] = $sell;
            $product[$key]['product'] = $this->get_sells_product($sell['ID']);
        }
        $res[] = $sells;
        $res[] = $product;
        return $res;
    }

    public function get_sells_product($id)
    {
        $product = Db::table('u_sell_d')
            ->where('MasterID', $id)
            ->order('Price', 'desc')
            ->select();
        return $product;
    }

    /**获取兑换信息
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function get_exchange()
    {
        $uuid = self::$uuid;
        $exchanges = Db::table('u_exchange')
            ->field('Member,Card,CreateDate,Remark,Point,ID')
            ->where('MemberID', $uuid)
            ->order('CreateDate', 'desc')
            ->limit(5)
            ->select();
        if (empty($exchanges)) {
            return false;
        }
        foreach ($exchanges as $key => $exchange) {
            $exchanges[$key]['product'] = $this->get_exchange_product($exchange['ID']);
        }
        return $exchanges;
    }

    /**
     * 获取完事兑换信息
     * @return array|bool
     */
    public function get_exchanges($date = '')
    {
        $uuid = self::$uuid;
        if (empty($date)) {
            $exchanges = Db::table('u_exchange')
                ->field('Member,Card,CreateDate,Remark,Point,ID')
                ->where('MemberID', $uuid)
                ->order('CreateDate', 'desc')
                ->paginate(15);
        } else {
            $exchanges = Db::table('u_exchange')
                ->field('Member,Card,CreateDate,Remark,Point,ID')
                ->where('MemberID', $uuid)
                ->where('CreateDate', 'between', [
                    $date['start_date'],
                    $date['end_date'],
                ])
                ->order('CreateDate', 'desc')
                ->paginate(15);
        }
        if (empty($exchanges[0])) {
            return $exchanges = false;
        }
        $point[] = '';
        foreach ($exchanges as $key => $exchange) {
            $point[$key] = $exchange;
            $point[$key]['product'] = $this->get_exchange_product($exchange['ID']);
        }
        $res[] = $exchanges;
        $res[] = $point;
        return $res;

    }

    /**
     * 获取兑换的商品列表
     * @param $id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function get_exchange_product($id)
    {
        $product = Db::table('u_exchange_d')
            ->alias('e')
            ->join('u_product p', 'e.ProductID = p.ID')
            ->field('e.Product,e.Number,e.Point,e.Remark,p.Price,e.Total,p.MemberPrice')
            ->where('e.MasterID', $id)
            ->order('e.Point', 'desc')
            ->select();
        return $product;
    }

    public function get_introducer()
    {
        $uuid = self::$uuid;
        $introducer = Db::table('r_point')
            ->alias('rp')
            ->join('u_point up', 'rp.PointID = up.ID')
            ->join('u_member m', 'rp.IntroducerID = m.ID')
            ->join('u_sell s', 'rp.SellID = s.ID', 'left')
            ->join('u_card c', 'm.CardID = c.ID')
            ->field('m.Member,up.CreateDate,.rp.SellID,up.Point,m.Level,m.Introducer,s.Card,s.Income,s.Point as spoint,c.Card,s.Remark')
            ->where('rp.MemberID', $uuid)
            ->order('up.CreateDate', 'desc')
            ->limit(5)
            ->select();
        return $introducer;
    }

    public function get_sale($date = '')
    {
        $uuid = self::$uuid;
        if (empty($date)) {
            $introducer = Db::table('r_point')
                ->alias('rp')
                ->join('u_point up', 'rp.PointID = up.ID')
                ->join('u_member m', 'rp.IntroducerID = m.ID')
                ->join('u_sell s', 'rp.SellID = s.ID', 'left')
                ->join('u_card c', 'm.CardID = c.ID')
                ->field('m.Member,up.CreateDate,.rp.SellID,up.Point,m.Level,m.Introducer,s.Card,s.Income,s.Point as spoint,c.Card,s.Remark')
                ->where('rp.MemberID', $uuid)
                ->order('up.CreateDate', 'desc')
                ->paginate(15);
        } else {
            $introducer = Db::table('r_point')
                ->alias('rp')
                ->join('u_point up', 'rp.PointID = up.ID')
                ->join('u_member m', 'rp.IntroducerID = m.ID')
                ->join('u_sell s', 'rp.SellID = s.ID', 'left')
                ->join('u_card c', 'm.CardID = c.ID')
                ->field('m.Member,up.CreateDate,rp.SellID,up.Point,m.Level,m.Introducer,s.Card,s.Income,s.Point as spoint,c.Card,s.Remark')
                ->where('rp.MemberID', $uuid)
                ->where('up.CreateDate', 'between', [
                    $date['start_date'],
                    $date['end_date'],
                ])
                ->order('up.CreateDate', 'desc')
                ->paginate(15);
        }
        if (empty($introducer[0])) {
            return false;
        }
        return $introducer;
    }

    public function get_offset()
    {
        $uuid = self::$uuid;
        $offset = Db::table('u_member')
            ->where('ID', $uuid)
            ->field('Level')
            ->find();
        return $offset;
    }

    public function get_user()
    {
        $uuid = self::$uuid;
        $user = Db::table('u_member')
            ->where('ID', $uuid)
            ->field('Member,Sex,Birthday,Age,Mobile,QQ,Email')
            ->find();
        $user['Birthday'] = strtotime($user['Birthday']);
        return $user;
    }

    public function get_card()
    {
        $uuid = self::$uuid;
        $card = Db::table('u_member')
            ->alias('m')
            ->join('u_card c', 'm.CardID = c.ID')
            ->where('m.ID', $uuid)
            ->find();
        return $card;
    }
}

